// packageRecruit/pages/pending/pending.js
import {  getMonthGroupList } from '../../api/recruit'
import user from "../../../utils/user"
// import user from '../../../utils/user.js'
import { toast,confirm } from '../../../utils/util.js'
Page({
    data: {
        list:{},
        companyId:'',
        searchText:''
    },
    static:{
        hasNextPage:false,
        pageSize:30,
        pageNum:1,
    },
    onLoad(options) {
        this.setData({
            companyId:options.companyId
        })
        user.waitLogin().then(async res=>{
            this.getMonthGroupList()
        })
        
    },
    onReady() {

    },
    onShow() {
        
    },
    // 吸顶事件监听
    onScrollSticky(event){
        const { date } = event.target.dataset
        if(event.detail.isFixed){
            this.data.list[date].isFixed = true 
            this.setData({
                list:this.data.list
            })
        }else{
            this.data.list[date].isFixed = false
            this.setData({
                list:this.data.list
            })
        }
    },
    async getMonthGroupList(){
        const result = await getMonthGroupList({
            pageSize: this.static.pageSize,
            pageNum: this.static.pageNum,
            searchText:this.data.searchText
        })
        for(let i in result.data){
            result.data[i].isFixed=false
		}
		let keys = []
		for(let key in result.data) {
			keys.push(key)
		}
		let arr = keys.sort((m, n) => {
			let j = m.replace('-', '')
			let k = n.replace('-', '')
			return  k * 1 - j * 1
		})
        const newObjKey = arr[0]
		const oldObjKey = Object.keys(this.data.list)[Object.keys(this.data.list).length-1]
        if(newObjKey === oldObjKey){
            this.data.list[oldObjKey].list.push(...result.data[newObjKey].list)
            delete result.data[newObjKey]
		}
		let obj1 = { ...this.data.list }
		arr.forEach(i => {
			obj1[i] = result.data[i]
		})
		var obj = {...this.data.list,...result.data}
        this.setData({
            list: obj1,
            hasNextPage:result.hasNextPage
        })
    },
    // 搜索
    onSearch(event){
        const { value } = event.detail
        this.static.pageNum = 1
        this.setData({
            searchText:value,
            list:[],
        })
        this.getMonthGroupList()
    },
    // 拨打电话
    onMakePhone(event){
        const { phone } = event.currentTarget.dataset
        wx.makePhoneCall({
            phoneNumber:phone
        })
    },
    threshold(){
        if(!this.data.hasNextPage){
            return
        }
        this.static.pageNum = this.static.pageNum+=1
        this.getMonthGroupList()
    },
})